#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:views.py
# author:wmf
# datetime:2021/6/1 21:47
# software: PyCharm
'''
this is function description
'''
# import module your need
from flask import request, render_template
from homework11.models import app, Message, db
from werkzeug.exceptions import BadRequestKeyError
from homework11.function import strike, to_json


@app.route('/')
def index():
    return "api server runing!"


@app.route('/show', methods=['GET', 'POST'])
def show():
    if request.method == 'POST':
        # Edit功能
        try:
            if request.form['data']:
                data = request.form['data']
                title = strike(data)
                if request.form['id']:
                    ID = request.form['id']
                    users = Message.query.all()
                    for user in users:
                        if user.autoID == int(ID):
                            user.status = "Done"
                            user.title1 = title[1]
                            user.title2 = title[0]
                            db.session.commit()
        except BadRequestKeyError:
            pass
        # Done和Undo功能
        try:
            if request.form['data1'] == 'Done':
                if request.form['id1']:
                    ID = request.form['id1']
                    users = Message.query.all()
                    for user in users:
                        if user.autoID == int(ID):
                            user.status = 'Undo'
                            title = strike(user.title1)
                            user.title1 = title[0]
                            user.title2 = title[1]
                            db.session.commit()
            elif request.form['data1'] == 'Undo':
                if request.form['id1']:
                    ID = request.form['id1']
                    users = Message.query.all()
                    for user in users:
                        if user.autoID == int(ID):
                            user.status = 'Done'
                            title = user.title1
                            user.title1 = user.title2
                            user.title2 = title
                            db.session.commit()
        except BadRequestKeyError:
            pass
        # Remove功能
        try:
            if request.form['id2']:
                ID = request.form['id2']
                users = Message.query.all()
                for user in users:
                    if user.autoID == int(ID):
                        db.session.delete(user)
                        db.session.commit()
        except BadRequestKeyError:
            pass
        # Add功能
        try:
            if request.form['add_Item']:
                item = request.form['add_Item']
                users = Message.query.all()
                length = len(users) + 1
                user = Message(autoID=length, title1=item, status='Done')
                db.session.add(user)
                db.session.commit()
        except BadRequestKeyError:
            pass
        # Search功能
        try:
            if request.form['search_Item']:
                item = request.form['search_Item']
                all_results = Message.query.filter(Message.title1.like("%" + item + "%")).all()
                message = to_json(all_results)
                condition = 1
                messages = Message.query.all()
                count = 1
                active = 0
                for data in messages:
                    data.autoID = count
                    count += 1
                    if data.status == "Done":
                        active += 1
                count = count - 1
                done = count - active
                info = [count, done, active]
                return render_template('message.html', condition=condition, message=message, info=info)
        except BadRequestKeyError:
            pass
    messages = Message.query.all()
    count = 1
    active = 0
    for message in messages:
        message.autoID = count
        count += 1
        db.session.commit()
        if message.status == "Done":
            active +=1
    data = to_json(messages)
    count = count-1
    done = count-active
    info = [count, done, active]
    return render_template('message.html', data=data, info=info)


if __name__ == '__main__':
    app.run()
